package com.kgc.community.controller;

import com.github.pagehelper.PageInfo;
import com.kgc.community.pojo.RoleMenu;
import com.kgc.community.pojo.User;
import com.kgc.community.pojo.Village;
import com.kgc.community.service.MenuService;
import com.kgc.community.service.UserService;
import com.kgc.community.service.VillageService;
import org.junit.Test;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

/**
 * @author Mr.man
 * @Date 2020-11-26
 */
@Controller
public class Logincontroller_man {
    @Resource
    UserService userService;
    /**
     * 获得菜单
     */
    @Resource
    MenuService menuService;

    @Resource
    VillageService villageService;

    /**
     * 进入登录界面
     * @return
     */
    @RequestMapping("gologin.do")
    public String gobuildinglst(){
        return "login";
    }

    /**
     * 1、登录界面
     * 2、加载菜单
     * @param model
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("login.do")
    public String login(Model model, String username, String password, HttpServletRequest request){
        User user = userService.login(username, password);
        //登录成功
        if(user!=null){
            request.getSession().setAttribute("user",user);
            //加载菜单
            List<RoleMenu> roleMenus = menuService.selectByRid(user.getRoleId());
            for (RoleMenu roleMenu : roleMenus) {
                System.out.println(roleMenu.getMenu().getName());
            }
            model.addAttribute("roleMenu",roleMenus);

            return "index";
        }
        //登录失败
        model.addAttribute("msg","登录失败");
        return "login";
    }

    @RequestMapping("welcome.do")
    public String welcome(){
        return "welcome";
    }

}
